# coding = utf-8
import logging

from django.db.models import Q
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from pypinyin import pinyin, Style

from base.api.serializers import UnitSerializer, StoreHouseSerializer, CategorySerializer, CustSuppClassSerializer, \
    GoodsSerializer, CustomSupplierSerializer, GoodsCodeSerializer
from base.models import Unit, StoreHouse, Category, CustSuppClass, Goods, CustomSupplier
from finance.models import Prepayment
from purchase.models import WareHouseEntry, PurchasePayOrder
from sale.models import SaleOrder, SaleReceipt
from store.models import GoodsTransfer

logger = logging.getLogger("django")


class UnitListAPIView(ListAPIView):
    """计量单位列表"""
    permission_classes = ()
    serializer_class = UnitSerializer
    queryset = Unit.objects.all()
    pagination_class = None


class StoreHouseListAPIView(ListAPIView):
    """仓库列表"""
    permission_classes = ()
    serializer_class = StoreHouseSerializer
    queryset = StoreHouse.objects.all()
    pagination_class = None


class CategoryTreeAPIView(ListAPIView):
    """商品类别"""
    permission_classes = ()
    serializer_class = CategorySerializer
    queryset = Category.objects.filter(parent__isnull=True)
    pagination_class = None


class CustSuppClassTreeAPIView(ListAPIView):
    """客户供应商分类"""
    permission_classes = ()
    serializer_class = CustSuppClassSerializer
    queryset = CustSuppClass.objects.filter(parent__isnull=True)
    pagination_class = None


class GoodsCodeListAPIView(ListAPIView):
    """商品编码"""
    permission_classes = ()
    serializer_class = GoodsCodeSerializer
    queryset = Goods.objects.all()
    pagination_class = None


class GoodsListAPIView(ListAPIView):
    """商品列表"""
    permission_classes = ()
    serializer_class = GoodsSerializer
    queryset = Goods.objects.all()
    pagination_class = None

    def get_queryset(self):
        queryset = super().get_queryset()

        cate_id = self.request.query_params.get("cate_id", None)
        parent_id = self.request.query_params.get("parent_id", None)
        name = self.request.query_params.get("name", None)

        if name:
            queryset = queryset.filter(Q(code__contains=name) | Q(name__contains=name) | Q(help_code__contains=name))

        if parent_id:
            if parent_id != "-1":
                items = Category.objects.filter(Q(path__contains=parent_id) | Q(id=int(parent_id))).values_list("id", flat=True)
                queryset = queryset.filter(cate_id__in=list(items))
        elif cate_id:
            queryset = queryset.filter(cate_id=int(cate_id))

        return queryset


class SupplierListAPIView(ListAPIView):
    """供应商"""
    permission_classes = ()
    serializer_class = CustomSupplierSerializer
    queryset = CustomSupplier.objects.filter(is_supplier=1)
    pagination_class = None


class CustomerListAPIView(ListAPIView):
    """客户"""
    # permission_classes = ()
    serializer_class = CustomSupplierSerializer
    queryset = CustomSupplier.objects.filter(is_customer=1)
    pagination_class = None


class CustomSupplierListAPIView(ListAPIView):
    """客户供应商"""
    permission_classes = ()
    serializer_class = CustomSupplierSerializer
    queryset = CustomSupplier.objects.all()
    pagination_class = None


class GoodsCodeAPIView(APIView):
    """生成商品编号"""
    # authentication_classes = ()
    permission_classes = ()

    def get(self, request, *args, **kwargs):

        max_code = Goods.get_max_code()
        code = {
            "max_code": max_code
        }
        return Response(code)


class CustomerSupplierCodeAPIView(APIView):
    """客商编号"""
    # authentication_classes = ()
    permission_classes = ()

    def get(self, request, *args, **kwargs):

        max_code = CustomSupplier.get_max_code()
        code = {
            "max_code": max_code
        }
        return Response(code)


class WareHouseEntryCodeAPIView(APIView):
    """入库单编号"""
    # authentication_classes = ()
    permission_classes = ()

    def get(self, request, *args, **kwargs):
        flag = request.query_params.get("flag")
        max_code = WareHouseEntry.get_max_code(int(flag))
        code = {
            "max_code": max_code
        }
        return Response(code)


class Hanzi2Pinyin(APIView):
    """汉字转拼音"""
    # authentication_classes = ()

    def get(self, request):
        name = request.query_params.get("name", None)
        name_pinyin = pinyin(name, style=Style.FIRST_LETTER)
        name_pinyin = [item[0] for item in name_pinyin]

        data = {
            'help_code': ''.join(name_pinyin).upper()
        }
        return Response(data)


class PurchasePayOrderCodeAPIView(APIView):
    """付款单编号"""
    # authentication_classes = ()
    permission_classes = ()

    def get(self, request, *args, **kwargs):
        max_code = PurchasePayOrder.get_max_code()
        code = {
            "max_code": max_code
        }
        return Response(code)


class SaleOrderCodeAPIView(APIView):
    """销售出库单编号"""
    # authentication_classes = ()
    permission_classes = ()

    def get(self, request, *args, **kwargs):
        flag = request.query_params.get("flag")
        max_code = SaleOrder.get_max_code(int(flag))
        code = {
            "max_code": max_code
        }
        return Response(code)


class SaleReceiptCodeAPIView(APIView):
    """销售收款单编号"""
    # authentication_classes = ()
    permission_classes = ()

    def get(self, request, *args, **kwargs):
        max_code = SaleReceipt.get_max_code()
        code = {
            "max_code": max_code
        }
        return Response(code)


class GoodsTransferCodeAPIView(APIView):
    """存储调拨编号"""
    # authentication_classes = ()
    permission_classes = ()

    def get(self, request, *args, **kwargs):
        max_code = GoodsTransfer.get_max_code()
        code = {
            "max_code": max_code
        }
        return Response(code)


class PrepaymentCodeAPIView(APIView):
    """预付款编号"""
    # authentication_classes = ()
    permission_classes = ()

    def get(self, request, *args, **kwargs):
        flag = request.query_params.get("flag")
        max_code = Prepayment.get_max_code(int(flag))
        code = {
            "max_code": max_code
        }
        return Response(code)
